#include "head.h"

void *handler(void *argc){
    threadPool_t* threadPool = (threadPool_t *)argc;
    
    pthread_mutex_lock(&threadPool->mutex);
    MYSQL *mysql = mysql_init(NULL);
    MYSQL *ret = mysql_real_connect(mysql, "localhost", "root", "123", "netdisk_v3", 3306, NULL, 0);
    pthread_mutex_unlock(&threadPool->mutex);
    
    if(ret == NULL){
        fprintf(stderr, "mysql error: %s\n", mysql_error(mysql));
        exit(EXIT_FAILURE);
    }

    while(1){
        // 加锁
        pthread_mutex_lock(&threadPool->mutex);
        while(threadPool->workQueue.len == 0 && threadPool->existStatus == 1){
            pthread_cond_wait(&threadPool->cond, &threadPool->mutex);
        }
        
        // 主线程要子线程去死
        if(threadPool->existStatus == 0){
            pthread_mutex_unlock(&threadPool->mutex);
            pthread_exit(NULL);
        }

        // 有任务到来
        int connectfd = threadPool->workQueue.head->connectfd;
        train_t mess = threadPool->workQueue.head->train;
        deQueue(&threadPool->workQueue);
        pthread_mutex_unlock(&threadPool->mutex);
        
        if(mess.type == LOGIN_USERNAME){
            recvRequest(connectfd, &mess);
        }

        // 未登录情况
        if(threadPool->recordUserId[connectfd] == 0){
            int ret = 0;
            if(mess.type == LOGIN_USERNAME){
                while((ret = loginVerity(mysql, connectfd, &mess)) <= 0){
                    if(ret == -1){
                        printf("other disconnect\n");
                        break;
                    }
                };
            }else{
                while((ret = signUpVerity(mysql, connectfd, &mess)) <= 0){
                    if(ret == -1){
                        printf("other disconnect\n");
                        break;
                    }
                };
            }
            if(ret == -1){
                continue;
            }
            // 登录或者注册验证完成,可以监听其发的指令请求了
            /* epollAdd(threadPool->efd, connectfd); */
            epollAddTime(threadPool->efd, connectfd, threadPool->timeOutArr);
            threadPool->recordUserId[connectfd] = ret;
            continue;
        }
        
        printf("gets and puts\n");
        printf("path: %s\n", mess.buf);
        // 处理上传和下载
        switch(mess.type){
        case GETS:
            getsOrder(mysql, threadPool->recordUserId[connectfd], connectfd, mess.buf, threadPool->dLdSvrSockfd);
            epollAdd(threadPool->efd, connectfd);
            break;
        case PUTS:
            putsOrder(mysql,threadPool->recordUserId[connectfd], connectfd, mess.buf);
            epollAdd(threadPool->efd, connectfd);
            break;
        }
    } 

}

// 功能：创建线程池
int makeWorker(threadPool_t *threadPool, int threadCount){
    // 初始化锁和条件变量
    pthread_mutex_init(&threadPool->mutex, NULL);
    pthread_cond_init(&threadPool->cond, NULL);

    // 线程信息
    threadPool->tidArr.arr = (threadInfo_t *)calloc(threadCount, sizeof(threadInfo_t));
    threadPool->tidArr.count = threadCount;

    // 与下载服务器建立连接
    threadPool->dLdSvrSockfd = establishConnectWithDownLoader(IP_ADDR1, PORT1);
    
    // 初始化存在变量
    threadPool->existStatus = 1;

    bzero(&threadPool->workQueue, sizeof(workqueue_t));
    bzero(threadPool->recordUserId, sizeof(int) * 1024);

    for(int i = 0; i < threadCount; i++){
        // 创建子线程
        pthread_t pid;
        pthread_create(&pid, NULL, handler, threadPool);

        // 填充线程相关信息
        threadPool->tidArr.arr[i].threadId = pid;
    }
    return 0;
}
